import request from "./request";
import { Code } from "@/enums";
import { message as $message, Modal } from 'ant-design-vue';
const UNKNOWN_ERROR = '未知错误，请重试';
export default {
    
    check: <T>(result: Result<T>): boolean => {
        
        let ok = result.code == Code.ok;
        if (!ok) {
            $message.error(result.message || UNKNOWN_ERROR);
            console.warn(result.message);
        }
        return ok;
    },
    get: <T>(url: string, data?: object): Promise<T> => {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                params: data,
                method: "get",
            })
                .then((res) => {
                    resolve(res as any);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
    post: <T>(url: string, data: object, config?: any): Promise<T> => {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: "POST",
                data: data,
                ...config
            })
                .then((res) => {
                    resolve(res as any);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
    put: <T>(url: string, data: object): Promise<T> => {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: "PUT",
                data: data,
            })
                .then((res) => {
                    resolve(res as any);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
    delete: <T>(url: string, data?: object): Promise<T> => {
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: "DELETE",
                params: data,
            })
                .then((res) => {
                    resolve(res as any);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
};
